package test {
	import flash.net.URLRequest;	
	import flash.events.Event;	
	import flash.display.Loader;	

	/**
	 * @author JamesXie
	 * @version 1.0
	 * @date Jan 21, 2011
	 * @name TestLoadImg 读取一幅图片
	 */
	public class TestLoadImg {
		private var _loader : Loader;

		private var _key : String;
		private var _url : String;
		private var _fun : Function;

		/**
		 * 读取一幅图片
		 * @param url 图片路劲
		 * @param fun 回调方法 参数是图片数据 fun(key,image)
		 */
		public function TestLoadImg(key : String = "",url : String = "",fun : Function = null) : void {
			_key = key;
			_url = url;
			_fun = fun;
			init();
		}

		private function init() : void {
			//trace("_key01 "+_key);
			//trace("_url01 "+_url);
			//trace("_url.length "+_url.length);
			
			if (_url.length > 0) {
				_loader = new Loader();
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
				_loader.load(new URLRequest(_url));
			}else {
				_fun(_key, null);
			}
		}

		private function onCompleteHandler(e : Event) : void {
			if (_loader.hasEventListener(Event.COMPLETE)) {
				_loader.removeEventListener(Event.COMPLETE, onCompleteHandler);
			}
			_fun(_key, _loader.content);
		}
	}
}